package com.ruoyi.project.sales.orders.search.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sales.orders.search.domain.Orders;
import com.ruoyi.project.sales.orders.search.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sales/orders/search")
public class OrdersController extends BaseController {

    private String prefix = "sales/orders/search";

    @Autowired
    private IOrderService orderService;

    @GetMapping("")
    public String operlog()
    {
        return prefix + "/search";
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Orders order)
    {
        startPage();
        List<Orders> list = orderService.selectOrderList(order);

        for (Orders orders:list){
            System.out.println(orders.getOrder_id ());
        }

        return getDataTable(list);
    }


    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add()
    {
            return prefix + "/create";
    }


    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated Orders orders)
    {
        return toAjax(orderService.insertOrder(orders));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer order_id, ModelMap map)
    {
        map.put("type", orderService.selectOrdersById(order_id));
        return prefix + "/update";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Orders orders)
    {
        /*if (UserConstants.TYPE__NAME_NOT_UNIQUE.equals(noticeTypeService.checkTypeNameUnique(noticeType)))
        {
            return error("修改类型'" + noticeType.getTname() + "'失败，类型名称已存在");
        }*/



        return toAjax(orderService.updateOrders(orders));
    }

    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(orderService.deleteOrdersByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


}
